<template>
  <a-modal
    title="添加关键结果"
    :visible="visible"
    :footer="null"
    :width="800"
    @cancel="hide"
  >
    <a-spin
      :spinning="loading"
    >
      <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="目标ID">
          <a-input v-model="form.missionId" disabled />
        </a-form-model-item>
        <a-form-model-item label="关键结果">
          <a-input v-model="form.content" />
        </a-form-model-item>
        <a-form-model-item label="到期日">
          <a-date-picker
            v-model="form.dueDate"
            type="date"
            placeholder="请选择到期日"
            style="width: 100%;"
          />
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 18, offset: 3 }" style="text-align: center">
          <a-button type="primary" @click="onSubmit">
            创建
          </a-button>
          <a-button style="margin-left: 10px;" @click="hide">
            取消
          </a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
// tool
import moment from 'moment'
// API
import { addKeyResult } from '@/api/mission'

export default {
  name: 'AddResult',
  data () {
    return {
      visible: false, // 是否显示模态框
      loading: false, // 是否加载
      labelCol: { offset: 3, span: 4 },
      wrapperCol: { span: 14 },
      form: {
        missionId: undefined,
        content: undefined,
        dueDate: undefined
      }
    }
  },
  methods: {
    show (record) {
      if (record.id) { this.form.missionId = record.id }
      this.record = record
      this.visible = true
    },
    hide () {
      this.visible = false
    },
    reset () {
      this.form.missionId = undefined
      this.form.content = undefined
      this.form.dueDate = undefined
      this.record = null
    },
    async onSubmit () {
      try {
        this.loading = true
        const params = { ...this.form }
        params.dueDate = moment(this.form.dueDate).format('YYYYMMDD')
        await addKeyResult(params)
        this.hide()
        this.$emit('finished', this.record)
        this.reset()
      } catch (error) {

      } finally {
        this.loading = false
      }
    }
  }
}
</script>
